package freebase;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Quadruple {

	/**
	 * @param args
	 */
	static String dir = "O:/unix/projects/pardosa/data06/clzhang";
	static String freebasedir = dir + "/freebase";
	static String wexdir = dir + "/wex";
	static String toyfreebase = dir + "/freebase_toy";

	public static void main2(String[] args) throws Exception {
		// TODO Auto-generated method stub
		cleanlines(freebasedir + "/" + "quadruples.tsv", toyfreebase
				+ "/quadruples.tsv");
//		BufferedReader br = ioUtility.FileAccess.OpenLineRead(freebasedir + "/"
//				+ "quadruples.tsv", "utf-8");
//		readsomelines(br, 100);
//		br.close();
	}

	public static String readsomelines(BufferedReader br, int n)
			throws Exception {
		String res = "";
		String line = "";
		int i = 0;
		int[] maxlen = new int[4];
		while ((line = br.readLine()) != null && i < n) {
			String[] abc = line.split("\t");
			for (int j = 0; j < abc.length; j++) {
				if (abc[j].length() > maxlen[j])
					maxlen[j] = abc[j].length();
			}
			System.out.println();
			res += line + "\n";
			i++;
		}
		return res;
	}

	public static void cleanlines(String infile, String outfile)
			throws Exception {
		BufferedReader br = ioUtility.FileAccess.OpenLineRead(infile, "utf-8");
		OutputStreamWriter osw = ioUtility.FileAccess.OpenWriteFile(outfile,
				"utf-8");
		String line = "";
		int[] maxLen = new int[4];
		int ln = 0;
		while ((line = br.readLine()) != null) {
			ln++;
			if (ln % 100000 == 0){
				System.out.print(".");
			}
			if(ln%10000000  == 0){
				System.out.println();
			}
			
			String[] abc = line.split("\t");
			for (int i = 0; i < abc.length; i++) {
				abc[i] = abc[i].replaceAll("/guid/", "");
				if (abc[i].length() > maxLen[i]) {
					maxLen[i] = abc[i].length();
				}
				osw.write(abc[i] + "\t");
			}
			osw.write("\n");
		}
		System.out.println("MAXLEN" + maxLen[0]+"\t"+maxLen[1]+"\t"+maxLen[2]+"\t"+maxLen[3]);
		br.close();
		osw.close();
	}

	public static void main(String[] args){
		  {
			    try
			    {
			      Connection conn;
			      Statement stmt;
			      ResultSet res;

			      Class.forName("com.mysql.jdbc.Driver").newInstance();

			      conn = DriverManager.getConnection("jdbc:mysql://pardosa06.cs.washington.edu/freebase",
			                                         "clzhang", "wiki");

			      stmt = conn.createStatement();
			      res = stmt.executeQuery(" select * from freebase_names limit 10");
			      stmt.executeUpdate(" insert into quadruples values ('a','b','c','d')");
			      while (res.next())
			      {
			        String name = res.getString("name");
			        System.out.println(name);
			      }
			      res.close();

			    }
			    catch (Exception ex)
			    {
			      System.out.println("Error : " + ex.toString());
			    }
			  }
	}
}
